grouprole = {}

grouprole.list = function(){
    main.init();
    $('#content').html(fly.template('http://naima.vn/grouprole/list.html', {
        grouproles: grouprole.get(),
        groups: grouprole.getGroups(),
        roles: grouprole.getRoles()
    }));
}

grouprole.get = function(){
    fly.ajax({
        service: '/grouprole/get',
        success: function(result){
            grouprole.grouproles = result.data;
        },
        async: false,
        loading: false
    });
    return grouprole.grouproles;
}

grouprole.getGroups = function(){
    fly.ajax({
        service: '/grouprole/getGroups',
        success: function(result){
            grouprole.groups = result.data;
        },
        async: false,
        loading: false
    });
    return grouprole.groups;
}

grouprole.getRoles = function(){
    fly.ajax({
        service: '/grouprole/getRoles',
        success: function(result){
            grouprole.roles = result.data;
        },
        async: false,
        loading: false
    });
    return grouprole.roles;
}

grouprole.search = function(groupId, roleId, grouproles){
    for(i=0; i<grouproles.length; i++){
        if(groupId == grouproles[i].groupId && roleId == grouproles[i].roleId){
            return true;
        }
    }
    return false;
}

grouprole.edit = function(){
    var grouproles = [];
    var i=0;
    $('input[type=checkbox]').each(function(){
        if($(this).is(':checked')){
            grouproles[i] = {};
            grouproles[i].groupId = parseInt($(this).attr('group'));
            grouproles[i].roleId = parseInt($(this).attr('role'));
            i++;
        }
    });
    
    popup.confirm("Bạn có chắc chắn muốn lưu danh sách phân quyền này?", function(){
        fly.ajax({
            service: '/grouprole/edit',
            data:{groupRoles:JSON.stringify(grouproles)},
            success: function(result){
                popup.msg(result.message);
            },
            method: 'POST'
        });
    });
}